package com.wangmeng.book.client;

import com.wangmeng.book.client.frame.BookPublishManage;
import com.wangmeng.book.client.frame.BookRecordManage;
import com.wangmeng.book.client.frame.IndexFrame;
import com.wangmeng.book.client.frame.LoginFrame;
import com.wangmeng.book.client.service.CommandService;
import com.wangmeng.book.server.Command;
import com.wangmeng.book.utils.ConstValueUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * User: 王猛
 * Date: 13-9-24
 */
public class MainFrame extends JFrame {
    private static final int LOGIN_DIALOG_WIDTH = 400;
    private static final int LOGIN_DIALOG_HEIGHT = 300;
    private static MainFrame mf;

    public MainFrame() {
        this.setTitle("文章管理系统");
        //设置菜单
        JMenuBar bar = new JMenuBar();
        this.setJMenuBar(bar);
        JMenu menu = new JMenu("系统");
        JMenuItem item = new JMenuItem("退出");
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CommandService.getInstance().logout();
                System.exit(0);
            }
        });
        menu.add(item);
        bar.add(menu);
        menu = new JMenu("管理");
        item = new JMenuItem("整理文档");
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                BookRecordManage brm = new BookRecordManage();
                mf.getContentPane().removeAll();
                mf.getContentPane().add(brm.getRoot());
                mf.doLayout();
                brm.getRoot().updateUI();
            }
        });
        menu.add(item);
        item = new JMenuItem("发布文档");
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                BookPublishManage brm = new BookPublishManage();
                mf.getContentPane().removeAll();
                mf.getContentPane().add(brm.getRoot());
                mf.doLayout();
                brm.getRoot().updateUI();
            }
        });
        menu.add(item);
        Integer pl = (Integer) CommandService.getInstance().getUserData("pri_level");
        if (pl != null && pl > 50) {
            item = new JMenuItem("更新目录");
            item.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (JOptionPane.showConfirmDialog(mf, "您确定要启动目录更新程序?", "操作确认", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
                        CommandService.getInstance().startJobByType(ConstValueUtils.JOB_LIST, 60, 0);
                    }
                }
            });
            menu.add(item);
        }
        bar.add(menu);
        this.getContentPane().add(new IndexFrame().getRoot());

        //字符串设置
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.setSize(Toolkit.getDefaultToolkit().getScreenSize());
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                CommandService.getInstance().logout();
            }
        });
        this.setVisible(true);
    }

    public static void main(String[] args) {
        JDialog dialog = new JDialog();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        dialog.setBounds(Double.valueOf((screenSize.getWidth() - LOGIN_DIALOG_WIDTH) / 2).intValue(), Double.valueOf((screenSize.getHeight() - LOGIN_DIALOG_HEIGHT) / 2).intValue(), LOGIN_DIALOG_WIDTH, LOGIN_DIALOG_HEIGHT);
        LoginFrame lf = new LoginFrame();
        lf.setDialog(dialog);
        dialog.getContentPane().add(lf.getRoot());
        dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        dialog.setModal(true);
        dialog.setVisible(true);
        if (CommandService.getInstance().isLogin()) {
//            System.out.println("Login Success!");
//            CommandService.getInstance().logout();
            dialog.dispose();
            mf = new MainFrame();
            mf.getJMenuBar().add(new JLabel("      登陆用户:" + CommandService.getInstance().getUserData("name")));
//            JToolBar bar = new JToolBar();
//            JLabel status = new JLabel(CommandService.getInstance().getUserData("name").toString());
//            bar.add(status);
//            mf.add(bar, BorderLayout.PAGE_END);
//            mf.doLayout();
        } else {
            System.exit(1);
        }
    }
}
